#!/bin/sh
KUBE_CONFIG_DIR=/opt/kubeconfig
CONFIG_FILE_NAME=kube-proxy.kubeconfig
CLUSTER_NAME=kubernetes
CLUSTER_USER=kube-proxy

node_name=k8s-node03
node_ip=172.16.0.234

export PATH=/opt/k8s/bin:$PATH

if [ ! -d ${KUBE_CONFIG_DIR} ]; then
    mkdir ${KUBE_CONFIG_DIR}
fi


echo "Setting cluster..."
kubectl config set-cluster ${CLUSTER_NAME} \
  --certificate-authority=/opt/cert/ca.pem \
  --embed-certs=true \
  --server="https://${node_ip}:6443" \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "Setting credentials..."
kubectl config set-credentials ${CLUSTER_USER} \
  --token=${BOOTSTRAP_TOKEN} \
  --client-certificate=/opt/cert/kube-proxy.pem \
  --client-key=/opt/cert/kube-proxy-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "setting context"
kubectl config set-context default \
  --cluster=${CLUSTER_NAME} \
  --user=${CLUSTER_USER} \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "Using context..."
kubectl config use-context default \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}
